//Drawing.cpp
// compile with: /EHsc /LD
#include "Including.h"

using namespace std;

namespace SDLFuncs
{
	//Rotation or Revolution for spheres
	GLfloat x_rot, y_rot;
	//Transformation Matrix
	GLfloat m1[16];
	//Transformation Matrix invoked from Static Math Library 
	float **matrix1;
	//Declare a matrix from static math lib
	Matrices::MyMatrices *a = new Matrices::MyMatrices();

	//////////////////////////
	/* Update Scene Elements
	*/	
	int MySDLFuncs::UpdateSceneElements(void *data)
	{
		//Profiling for FPS
		//cout<<Profiling::MyProfiling::CallFrequency()<<endl;


		//-------Animating MD2 models----------//
		/*
		glPushMatrix();
		static int n = 0;
		static float interp = 0.0;
		static double current_time = 0;
		static double last_time = 0;
		last_time = current_time;
		current_time = (double)glutGet (GLUT_ELAPSED_TIME) / 1000.0;
		interp += 10 * (current_time - last_time); //Animate model from frames 0 to num_frames-1
		MD2Models::MyMD2Models::Animate (1, md2file.header.num_frames - 1, &n, &interp);
		//MD2Models::MyMD2Models::Animate (96, 112, &n, &interp); //run
		glTranslatef(-8,0,-4);
		glRotatef(90,0,1,0);
		glRotatef(-90,1,0,0);
		glScalef(0.1,0.1,0.1);
		MD2Models::MyMD2Models::RenderFrameItp(n,interp,&md2file);
		glPopMatrix();
		//MD2Models::MyMD2Models::RenderFrame(n,&md2file);
		*/
		/*
		glEnable(GL_TEXTURE_2D);
		float percent = 0.03f;
		glPushMatrix();
		glTranslatef(-15,-2,-30);
		glPushMatrix();
		glScalef(0.08,0.1,0.08);
		glRotatef(90.0f, -1.0f, 0.0f, 0.0f);
		//glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
		myModel->SetState(MODEL_RUN);
		switch (myModel->GetState())
		{
		case MODEL_IDLE:
			myModel->Animate(0, 39, percent);
			gunModel->Animate(0, 39, percent);
			break;
		case MODEL_RUN:
			myModel->Animate(40, 46, percent);
			gunModel->Animate(40, 46, percent);
			break;
		case MODEL_CROUCH:
			myModel->Animate(136, 154, percent);
			gunModel->Animate(136, 154, percent);
			break;
		case MODEL_JUMP:
			myModel->Animate(67, 73, percent);
			gunModel->Animate(67, 73, percent);
			break;
		default:
			break;
		}
		glPopMatrix();
		glPopMatrix();
		glDisable(GL_TEXTURE_2D);
		*/

		//-------Collada Model---------//
		glPushMatrix();
		glRotatef(-180,0,1,0);
		glTranslatef(0,-5,-5);
		glRotatef(-90,1,0,0);
		Collada::MyCollada::RenderCollada(&colladafile);
		glPopMatrix();

		//Lock
		//SDL_SemWait( threadLock );

		//Swap Buffers
		//SDL_GL_SwapBuffers();
		
		//Unlock
		//SDL_SemPost( threadLock );
		
		return 0;
	}

	//////////////////////////
	/* Drawing Scene
	*/	
	int MySDLFuncs::RenderScene(void *data)
	{

		glEnable(GL_TEXTURE_2D);	
		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
		glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
		
		glPushMatrix();
		glTranslatef(0, 2, 2);
		glScalef(1.5,1.5,1.5);
		///////////////////////////////////////////////////
		glBindTexture(GL_TEXTURE_2D, texture5);
		//1.0 Place Model(Wire Sphere) using Matrix Transformation
		glPushMatrix();
		//1.1 Revolution
		memcpy(m1, a -> RotateMatrix(1,-x_rot), sizeof(m1));
		glMultMatrixf(m1);
		//1.2 Rotation
		glPushMatrix();	
		memcpy(m1, a -> RotateMatrix(1,-x_rot), sizeof(m1));
		glMultMatrixf(m1);
		memcpy(m1, a -> TranslateMatrix(1.5,2.0,0), sizeof(m1));
		glMultMatrixf(m1);
		glutSolidSphere(0.3, 32, 32); //Draws wire sphere
		glPopMatrix(); //End Rotation
		glPopMatrix(); //End Revolution
		//////////////////////////////////////////////////////////

		//Wire Sphere
		glBindTexture(GL_TEXTURE_2D, texture4);
		glPushMatrix();
		glTranslatef(-2, 5, 0.0);
		glRotatef(x_rot*4, 0.0, 1.0, 0.0);
		glRotatef(y_rot, 0.0, 1.0, 1.0);
		glutSolidSphere(1.0, 32, 32);
		glPopMatrix();
		
		//Solid Textured Sphere
		//glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, texture1);
	
		//glEnable(GL_TEXTURE_GEN_S);
		//glEnable(GL_TEXTURE_GEN_T);
		//glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
		//glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
	
		glPushMatrix();
		glTranslatef(3, 5, 0.0);
		glRotatef(x_rot*0.5, 0.0, 1.0, 0.0);
		glRotatef(y_rot*0.5, 0.0, 1.0, 1.0);
		glutSolidSphere(1.5, 32, 32);
		glPopMatrix();
		//glDisable(GL_TEXTURE_2D);
	
		//Textured Sphere
		//glEnable(GL_TEXTURE_2D);
		glBindTexture(GL_TEXTURE_2D, texture);

		glPushMatrix();
		glTranslatef(0.0, 2.0, 0.0);
		glRotatef(x_rot, 0.0, 1.0, 0.0);
		glRotatef(y_rot, 0.0, 1.0, 1.0);
		glutSolidSphere(1.0, 32, 32);
		glPopMatrix();
		glDisable(GL_TEXTURE_GEN_S);
		glDisable(GL_TEXTURE_GEN_T);
		glDisable(GL_TEXTURE_2D);
		
		//rotate all spheres
		x_rot = x_rot + 0.1;
		//y_rot = y_rot + 0.1;

		glPopMatrix();

		//Lock
	    //SDL_SemWait( threadLock );
	
		//Swap Buffers
		//SDL_GL_SwapBuffers();	

		//Unlock
		//SDL_SemPost( threadLock );

		return 0;
	}



}